package com.samphanie.auiu

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.google.gson.Gson
import com.samphanie.auiu.common.utils.LocalCache
import com.samphanie.auiu.library.log.AuiuConsolePrinter
import com.samphanie.auiu.library.log.AuiuLogConfig
import com.samphanie.auiu.library.log.AuiuLogConfig.JsonParser
import com.samphanie.auiu.library.log.AuiuLogManager
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta

/**
 * @author Dries
 **/
class AuiuApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        initARouter()
        initBugly()

        LocalCache.initialize(this)

        AuiuLogManager.init(object : AuiuLogConfig() {
            override fun injectJsonParser(): JsonParser {
                return JsonParser { src -> Gson().toJson(src) }
            }

            override fun getGlobalTag(): String {
                return "AuiuApplication"
            }

            override fun enable(): Boolean {
                return true
            }
        }, AuiuConsolePrinter())
    }

    private fun initBugly() {
        Bugly.init(this, "d63c0f9572", true)
        Bugly.setIsDevelopmentDevice(this, true)
    }

    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(base)
        Beta.installTinker()
    }
}